package com.gla.market.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.TintTypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.gla.market.activity.Commodity_FYHActivity;
import com.gla.market.activity.Shop_FYHActivity;
import com.gla.market.entity.ShopCart;
import com.gla.market.fragments.Shopcart;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.gla.market.R;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 购物车Adapter
 */
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder> {

    private List<ShopCart> shoppingCartBeanList;

    private Context context;

    public ShoppingCartAdapter(Context context) {
        this.context = context;
        //实例化ImageLoader
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
        ImageLoader.getInstance().init(configuration);
    }

    public void setShoppingCartBeanList(List<ShopCart> shoppingCartBeanList) {
        this.shoppingCartBeanList = shoppingCartBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopCart shopCart = shoppingCartBeanList.get(position);
        if (shopCart.getChoosed()) {
            holder.ckOneChose.setChecked(true);
        } else {
            holder.ckOneChose.setChecked(false);
        }

        holder.tvCommodityType.setText(shopCart.getCommodity().getType().getName());
        holder.tvCommodityName.setText(shopCart.getCommodity().getName());
        //设置点击跳转商品详情
        holder.tvCommodityName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Commodity_FYHActivity.class);
                intent.putExtra("id",String.valueOf(shopCart.getCommodity().getId()));
                context.startActivity(intent);
            }
        });
        DecimalFormat df = new DecimalFormat("0.00");
        String price = df.format(shopCart.getCommodity().getPrice());
        holder.tvCommodityPrice.setText("￥"+price);
        holder.tvCommodityNum.setText(" X" + shopCart.getNumber() + "");
        holder.tvCommodityShowNum.setText(shopCart.getNumber() + "");
        ImageLoader.getInstance().displayImage(shopCart.getCommodity().getPhoto(),holder.ivShowPic);
        //设置点击跳转商品详情
        holder.ivShowPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Commodity_FYHActivity.class);
                intent.putExtra("id",String.valueOf(shopCart.getCommodity().getId()));
                context.startActivity(intent);
            }
        });
        //单选框按钮
        holder.ckOneChose.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        shopCart.setChoosed(((CheckBox) v).isChecked());
                        Intent intent = new Intent("com.gla.market.CHECK");
                        intent.putExtra("shopcart_id",shopCart.getId());
                        intent.putExtra("checked",((CheckBox) v).isChecked());
                        context.sendBroadcast(intent);
                        //checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                    }
                }
        );
        //增加按钮
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.gla.market.ADD");
                intent.putExtra("shopcart_id",shopCart.getId());
                context.sendBroadcast(intent);
            }
        });
        //删减按钮
        holder.ivSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.gla.market.REDUCE");
                intent.putExtra("shopcart_id",shopCart.getId());
                context.sendBroadcast(intent);
            }
        });
        //删除弹窗
        holder.tvCommodityDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog alert = new AlertDialog.Builder(context).create();
                alert.setTitle("操作提示");
                alert.setMessage("您确定要将商品从购物车中移除吗？");
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        });
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent("com.gla.market.DELETE");
                                intent.putExtra("shopcart_id",shopCart.getId());
                                context.sendBroadcast(intent);
                            }
                        });
                alert.show();
            }
        });
        holder.tvCommodityName.setVisibility(View.VISIBLE);
        //是宠物的话隐藏数量修改框
        if(shopCart.getCommodity().getP_o_u()==1){
            holder.rlEdit.setVisibility(View.VISIBLE);
        } else {
            holder.rlEdit.setVisibility(View.GONE);
        }

        holder.tvCommodityNum.setVisibility(View.VISIBLE);
        holder.tvCommodityDelete.setVisibility(View.VISIBLE);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
    }

    //初始化控件
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivShowPic, tvCommodityDelete;
        TextView tvCommodityName, tvCommodityType, tvCommodityPrice, tvCommodityNum, tvCommodityShowNum, ivSub, ivAdd;
        CheckBox ckOneChose;
        LinearLayout rlEdit;

        public ViewHolder(View itemView) {
            super(itemView);
            ckOneChose = itemView.findViewById(R.id.ck_shopcart_chose);
            ivShowPic = itemView.findViewById(R.id.iv_show_pic);
            ivSub = itemView.findViewById(R.id.iv_sub);
            ivAdd = itemView.findViewById(R.id.iv_add);
            tvCommodityName = itemView.findViewById(R.id.tv_commodity_name);
            tvCommodityType = itemView.findViewById(R.id.tv_commodity_type);
            tvCommodityPrice = itemView.findViewById(R.id.tv_commodity_price);
            tvCommodityNum = itemView.findViewById(R.id.tv_commodity_num);
            tvCommodityShowNum = itemView.findViewById(R.id.tv_commodity_show_num);
            tvCommodityDelete = itemView.findViewById(R.id.tv_commodity_delete);
            rlEdit = itemView.findViewById(R.id.rl_edit);
        }
    }
}
